package com.lpw.dbvideo.user.provider;

import com.lpw.dbvideo.user.domain.User;
import org.apache.ibatis.jdbc.SQL;

/**
 * User 构建动态sql语句
 */
public class UserProvider {

    /**
     * 新增用户动态sql语句
     * @param user 新增的user对象
     * @return
     */
    public String insertUser(final User user){
        return new SQL(){
            {
                // 需要新增数据的表名
                INSERT_INTO("user");

                // 判断新增的内容是否为空
                if(user.getId() != null){
                    VALUES("id","#{id}");
                }
                if(user.getOpenid() != null){
                    VALUES("openid","#{openid}");
                }
                if(user.getName() != null){
                    VALUES("name","#{name}");
                }
                if(user.getSex() != null){
                    VALUES("sex","#{sex}");
                }
                if(user.getHeadImg() != null){
                    VALUES("head_img","#{headImg}");
                }
                if(user.getPhone() != null){
                    VALUES("phone","#{phone}");
                }
                if(user.getCity() != null){
                    VALUES("city","#{city}");
                }
                if(user.getSign() != null){
                    VALUES("sign","#{sign}");
                }
                if(user.getCreateTime() != null){
                    VALUES("create_time","#{createTime}");
                }
            }
        }.toString();
    }


    /**
     * 更新用户动态sql语句
     * @param user 更新的用户
     * @return
     */
    public String updateUser(final User user){
        return new SQL(){
            {
                // 要更新的表名
                UPDATE("user");

                // 更新条件
                if(user.getOpenid() != null){
                    SET("openid=#{openid}");
                }
                if(user.getName() != null){
                    SET("name=#{name}");
                }
                if(user.getSex() != null){
                    SET("sex=#{sex}");
                }
                if(user.getHeadImg() != null){
                    SET("head_img=#{headImg}");
                }
                if(user.getPhone() != null){
                    SET("phone=#{phone}");
                }
                if(user.getCity() != null){
                    SET("city=#{city}");
                }
                if(user.getSign() != null){
                    SET("sign=#{sign}");
                }
                if(user.getCreateTime() != null){
                    SET("create_time=#{createTime}");
                }

                WHERE("id=#{id}");
            }
        }.toString();
    }
}
